Fix the check for non-existence of the save/restore directories, by using &&
authoremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Wed, 26 Oct 2005 15:59:13 +0000 (16:59 +0100)
committeremellor@leeni.uk.xensource.com <emellor@leeni.uk.xensource.com>
Wed, 26 Oct 2005 15:59:13 +0000 (16:59 +0100)
rather than -a.  The former shortcuts at the script level, whereas the latter
does not, which means that the backtick-enclosed arguments are evaluated
regardless of the success of preceding tests.

Tidy the aforementioned test into a function of its own.

Add an implementation of usleep for those systems that only have sleep (Debian,
for example).

Signed-off-by: Ewan Mellor <ewan@xensource.com>
tools/examples/init.d/xendomains

index ce9628493785509430cd3e7e613482e18360a798..4d7db36295562f20435679e281a601a35698982b 100644 (file)
@@ -121,9 +121,34 @@ else
     }
 fi
 
+if ! which usleep >&/dev/null
+then
+  usleep()
+  {
+    if [ -n "$1" ]
+    then
+      sleep $(( $1 / 1000 ))
+    fi
+  }
+fi
+
 # Reset status of this service
 rc_reset
 
+##
+# Returns 0 (success) if the given parameter names a directory, and that
+# directory is not empty.
+#
+contains_something()
+{
+  if [ -d "$1" ] && [ `/bin/ls $1 | wc -l` -gt 0 ]
+  then
+    return 0
+  else
+    return 1
+  fi
+}
+
 # read name from xen config file
 rdname()
 {
@@ -133,7 +158,8 @@ rdname()
 rdnames()
 {
     NAMES=
-    if test ! -d $XENDOMAINS_AUTO -o `/bin/ls $XENDOMAINS_AUTO | wc -l` -eq 0; then 
+    if ! contains_something "$XENDOMAINS_AUTO"
+    then 
        return
     fi
     for dom in $XENDOMAINS_AUTO/*; do
@@ -177,9 +203,10 @@ start()
        return; 
     fi
 
-    if test "$XENDOMAINS_RESTORE" = "true" -a -n "$XENDOMAINS_SAVE" \
-           -a -d $XENDOMAINS_SAVE -a `/bin/ls $XENDOMAINS_SAVE | wc -l` -gt 0; then
-
+    if [ "$XENDOMAINS_RESTORE" = "true" ] &&
+       contains_something "$XENDOMAINS_SAVE"
+    then
+        mkdir -p $(dirname "$LOCKFILE")
        touch $LOCKFILE
        echo -n "Restoring Xen domains:"
        for dom in $XENDOMAINS_SAVE/*; do
@@ -195,9 +222,8 @@ start()
        done
     fi
 
-    if test -n "$XENDOMAINS_AUTO" -a -d $XENDOMAINS_AUTO \
-           -a `/bin/ls $XENDOMAINS_AUTO | wc -l` -gt 0; then
-
+    if contains_something "$XENDOMAINS_AUTO"
+    then
        touch $LOCKFILE
        echo -n "Starting auto Xen domains:"
        # We expect config scripts for auto starting domains to be in
@@ -378,8 +404,10 @@ check_domain_up()
 
 check_all_auto_domains_up()
 {
-    if test -z "$XENDOMAINS_AUTO" -o ! -d "$XENDOMAINS_AUTO" \
-           -o `/bin/ls $XENDOMAINS_AUTO | wc -l` -eq 0; then return 0; fi
+    if ! contains_something "$XENDOMAINS_AUTO"
+    then
+      return 0
+    fi
     missing=
     for nm in $XENDOMAINS_AUTO/*; do
        rdname $nm
@@ -399,8 +427,10 @@ check_all_auto_domains_up()
 
 check_all_saved_domains_up()
 {
-    if test -z "$XENDOMAINS_SAVE" -o ! -d "$XENDOMAINS_SAVE" \
-           -o `/bin/ls $XENDOMAINS_SAVE | wc -l` -eq 0; then return 0; fi
+    if ! contains_something "$XENDOMAINS_SAVE" 
+    then
+      return 0
+    fi
     missing=`/bin/ls $XENDOMAINS_SAVE`
     echo -n " MISS SAVED: " $missing
     return 1